<template>
  <div class="tutorial-page">
    <div class="tutorial-category-tab">
      <ul class="w-1200">
        <li :class="{ active: selectUuid === '' }" @click="handleFilterCategory('')">
          <span style="position: relative; top: 2px;">全部</span>
        </li>
        <li
          v-for="item in categoryList"
          :key="item.id"
          :class="{ active: item.id === selectUuid }"
          @click="handleFilterCategory(item.id)"
          >
          <span :class="{ 'all-en': !/[\u4E00-\u9FA5]/.test(item.name) }">{{ item.name }}</span>
        </li>
      </ul>
    </div>
    <div class="tutorial-content w-1200">
      <a-skeleton :loading="requestLoading" active>
        <div class="tutorial-list-ul" v-if="tutorialList.length">
          <nuxt-link :to="`/tutorial/${item.id}`" target="_blank" class="tutorial-list-ul-li" v-for="item in tutorialList" :key="item.id">
            <img :src="item.headImg" alt="" class="left-logo">
            <div class="right-info">
              <p class="title">{{ item.name }}</p>
              <div class="author">
                <img :src="item.ownerAvatar" alt="" class="avatar">
                <span>{{ item.ownerNickname }}</span>
                <span v-if="item.ownerJob">{{ item.ownerJob }}</span>
              </div>
              <div class="paragraph">
                <i class="ri-article-line"></i>
                <em>{{ item.nodeNumb }}个节点</em>
              </div>
              <div class="desc">{{ item.summary }}</div>
            </div>
          </nuxt-link>
        </div>
        <a-empty
          v-else
          style="margin-top: 200px;"
          :image="require('~/assets/images/nodata.png')"
          description="暂无教程" />
      </a-skeleton>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TutorialPage',
  layout: 'BaseLayout',
  async asyncData ({ $axios }) {
    try {
      const { data } = await $axios.get('/gimishare/api/v1/tutorial/category')
      const { data: tutorial } = await $axios.post('/gimishare/api/v1/tutorial/list', {
        categoryId: ''
      })
      return {
        categoryList: data,
        tutorialList: tutorial
      }
    } catch (e) {}
  },
  data () {
    return {
      requestLoading: false,
      selectUuid: '',
      categoryList: [],
      tutorialList: []
    }
  },

  methods: {
    async findTutorialList () {
      this.requestLoading = true
      try {
        const { data } = await this.$axios.post('/gimishare/api/v1/tutorial/list', {
          categoryId: this.selectUuid
        })
        this.tutorialList = data
      } finally {
        this.requestLoading = false
      }
    },

    handleFilterCategory (cateUuid) {
      if (this.selectUuid === cateUuid) return
      this.selectUuid = cateUuid
      this.findTutorialList()
    },

    linkTutorialChapter (uuid) {
      this.$router.push(`/tutorial/${uuid}/intro`)
    }
  }
}
</script>
<style lang="less" scoped>
.tutorial-page {
  display: flex;
  flex-direction: column;
  margin-top: -1px;
  .tutorial-category-tab {
    background-color: #fff;
    border-bottom: 1px solid #f1f1f1;
    position: sticky;
    top: 60px;
    z-index: 99;
    ul {
      list-style: none;
      margin: 0 auto;
      padding: 10px 0;
      display: flex;
      li {
        cursor: pointer;
        border-radius: 4px;
        margin-right: 12px;
        padding: 0 12px;
        height: 30px;
        display: flex;
        align-items: center;
        &.active {
          background-color: var(--select-primary-color);
          span {
            color: var(--primary-color);
          }
        }
        span {
          font-size: 13px;
          color: #4c4c4c;
          letter-spacing: 1px;
          &.all-en {
            font-size: 14px;
          }
        }
      }
    }
  }
  .tutorial-content {
    margin-top: 12px;
    border-radius: 4px;
    ::v-deep .ant-skeleton-content {
      background-color: #fff;
      padding: 20px 40px 30px;
    }
    .tutorial-list {
      border-radius: 4px;
      &-ul {
        display: flex;
        flex-wrap: wrap;
        &-li {
          width: ~"calc(50% - 6px)";
          margin: 0 12px 12px 0;
          padding: 18px 24px;
          border-radius: 6px;
          display: flex;
          align-items: flex-start;
          background-color: #fff;
          transition: 0.3s background-color;
          &:hover {
            background-color: #fafafa;
          }
          &:nth-child(2n) {
            margin-right: 0;
          }
          .left-logo {
            width: 125px;
            height: 156px;
            object-fit: cover;
            font-size: 0;
            border-radius: 4px;
          }
          .right-info {
            padding: 12px 0 0 12px;
            .title {
              font-size: 17px;
              font-weight: 600;
              color: #000;
            }
            .author {
              display: flex;
              align-items: center;
              padding: 8px 0;
              .avatar {
                width: 24px;
                height: 24px;
                object-fit: cover;
                border-radius: 4px;
                margin-right: 6px;
              }
              span {
                color: #4c4c4c;
                margin-right: 16px;
                position: relative;
                &:not(:last-child)::after {
                  content: "·";
                  color: #999;
                  position: absolute;
                  right: -10px;
                  top: 0px;
                  font-weight: 600;
                }
              }
            }
            .paragraph {
              display: flex;
              align-items: center;
              color: #999;
              i {
                font-size: 18px;
                position: relative;
                top: 1px;
              }
              em {
                font-style: normal;
                margin-left: 6px;
              }
            }
            .desc {
              color: #999;
              margin-top: 8px;
              -webkit-box-orient: vertical;
              display: block;
              display: -webkit-box;
              max-width: 100%;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: normal;
              -webkit-line-clamp: 2;
            }
          }
        }
      }
    }
  }
}
</style>
